﻿using QRCoder;
using ScintillaNET;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace TextToQRCode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.StartPosition = FormStartPosition.CenterScreen;//启动在屏幕中央
            InitializeComponent();
        }
        //文本输入（或内容编辑、文本编辑区）
        //二维码展示（或QR码视图、生成与展示）
        //分段二维码（或多码展示、长文本二维码分割）
        private void Form1_Load(object sender, EventArgs e)
        {
            initTextArea(scintilla1);
        }


        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            // 输出发送者信息，便于调试
            Console.WriteLine("Sender: " + sender.ToString());

            // 检查当前选择的标签页
            TabPage selectedTab = e.TabPage;
            int qrSize = Math.Min(selectedTab.ClientSize.Width, selectedTab.ClientSize.Height);

            if (selectedTab == this.tabPage1)
            {
                // 如果是 tabPage1
                Console.WriteLine("TabPage1 is selected.");
                pictureBox1.Image = null;

            }
            else if (selectedTab == this.tabPage2)
            {
                // 如果是 tabPage2
                Console.WriteLine("TabPage2 is selected.");
                label1.Text = "";
                try
                {
                    DisplayQRCode(qrSize);
                }
                catch (Exception ex)
                {
                    // 将异常详细信息添加到 label1 中
                    //label1.Text = $"Error: {ex.Message}\n{ex.StackTrace}";
                    label1.Text = $"Error: 数据过长";
                    label1.ForeColor = Color.Red; // 设置 label1 文字颜色为红色以突出显示错误
                }

            }
        }



        private void DisplayQRCode(int size)
        {
            string text = scintilla1.Text;
            if (!string.IsNullOrEmpty(text))
            {
                GenerateQRCode(text, size);
            }
        }

        private void GenerateQRCode(string text, int size)
        {
            using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
            {
                QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.L);
                using (QRCode qrCode = new QRCode(qrCodeData))
                {
                    int cnt = qrCodeData.ModuleMatrix.Count;
                    int pixelsPerModule = size / cnt;
                    Bitmap qrCodeImage = qrCode.GetGraphic(pixelsPerModule);
                    pictureBox1.Image = qrCodeImage;
                }
            }
        }


        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //int l = textBox1.SelectionLength;
            //if (l == 0)
            //{
            //    textBox1.SelectAll();
            //}
            //textBox1.Copy();
            //textBox1.SelectionStart = 0;
            //textBox1.SelectionLength = 0;

            int l = scintilla1.Selections.Count;
            if (l == 0)
            {
                scintilla1.SelectAll();
                scintilla1.Copy();
                scintilla1.ClearSelections();
            }
            else
            {
                scintilla1.Copy();
            }

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            scintilla1.Text = "";
            scintilla1.Paste();
            scintilla1.CurrentPosition = 0;
            scintilla1.SelectionEnd = 0;
            scintilla1.FirstVisibleLine = 0;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            scintilla1.Text = "";
        }


        //
        //初始化TextArea
        //
        private void initTextArea(ScintillaNET.Scintilla scintilla1)
        {
            scintilla1.EolMode = ScintillaNET.Eol.Lf;
            // 
            // TextArea
            // 
            //this.scintilla1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            //this.scintilla1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.scintilla1.Font = new System.Drawing.Font("Consolas", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            //this.scintilla1.Location = new System.Drawing.Point(0, 0);
            //this.TextArea.Multiline = true;

            //this.TextArea.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.scintilla1.Size = new System.Drawing.Size(400, 376);
            this.scintilla1.TabIndex = 0;
            //this.TextArea.WordWrap = false;
            //this.scintilla1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            //
            //
            scintilla1.Styles[Style.Default].Font = "Consolas";
            scintilla1.Styles[Style.Default].Size = 14;
            //显示当前行背景颜色
            scintilla1.CaretLineVisible = true;

            InitNumberMargin(scintilla1);
        }

        //
        //初始化显示行号
        //
        private void InitNumberMargin(ScintillaNET.Scintilla scintilla1)
        {
            //ScintillaNET.MarginCollection ms = TextArea.Margins;
            //String msg = "ms.Count=" + ms.Count;
            //foreach (ScintillaNET.Margin m in ms)
            //{
            //    msg+="\n" + m.Width;
            //}
            //MessageBox.Show(msg);

            int NUMBER_MARGIN = 0;
            var nums = scintilla1.Margins[NUMBER_MARGIN];
            nums.Width = 32;
            nums.Type = MarginType.Number;
            nums.Sensitive = true;
            nums.Mask = 0;
        }

        private void scintilla1_TextChanged(object sender, EventArgs e)
        {
            this.scintilla1.ScrollWidth = 5;
            this.scintilla1.ScrollWidthTracking = true;

            int totalLines = scintilla1.Lines.Count;
            string strNumber = totalLines.ToString();
            int length = strNumber.Length;
            UpdateNumberMargin(scintilla1, length);
        }


        private void UpdateNumberMargin(ScintillaNET.Scintilla scintilla1, int length)
        {
            int NUMBER_MARGIN = 0;
            var nums = scintilla1.Margins[NUMBER_MARGIN];
            if (length > 1)
            {
                nums.Width = length * 15;
            }
            else
            {
                nums.Width = length * 20;
            }

            nums.Type = MarginType.Number;
            nums.Sensitive = true;
            nums.Mask = 0;
        }
    }
}
